<?php

class MenuController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/main';
	
	public $errMsg = null;
	
	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view','photo', 'fav', 'news', 'content', 
					'question', 'suggest', 'favc', 'addSuggest', 'captcha', 'create', 
					'regist', 'member', 'logout', 'login', 'order', 'order2', 'order3', 'order4'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
	
	public function actionOrder()
	{
		$this->layout = "//layouts/view_order";
		$this->render('order');
	}
	
	public function actionOrder2()
	{
		$this->layout = "//layouts/view_order";
		$this->render('order2');
	}
	
	public function actionOrder3()
	{
		$this->layout = "//layouts/view_order";
		$this->render('order3');
	}
	
	public function actionOrder4()
	{
		$this->layout = "//layouts/view_order";
		$this->render('order4');
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Menu;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Menu']))
		{
			$model->attributes=$_POST['Menu'];
			
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('photo',array(
			'model'=>$model,
		));
	}
	
	public function getRoomList()
	{
		$data =  new CActiveDataProvider('Content', array(
			'criteria'=>array(
				'condition'=>'owner_menu_id=2'
			),
			'pagination'=>array(
				'pageSize'=>100
			)
		));
		
		return $data->getData();
	}

	public function actionLogout()
	{
		if (!empty($_SESSION["userName"]))
		{
			$_SESSION["userName"] = null;
		}
		echo '<script type="text/javascript">window.location.href="index.php";</script>';
	}

	public function actionMember()
	{
		$this->layout = "//layouts/view_no_sub_menu";
		$this->render('member');
	}
	
	public function actionLogin()
	{
		$model=new User;

		if(isset($_POST['User']))
		{
			$model->attributes=$_POST['User'];
			$user = $this->getUserByName($model->user_name)->getData();
			if (empty($user))
			{
				echo '登录失败：该用户不存在！';
			}
			else 
			{
				if ($user[0]->password === md5($model->password))
				{
					$_SESSION["userName"] = $model->user_name;
					echo 'success';
				}
				else
				{
					echo '登录失败，用户名或密码错误！';
				}
			}
		}
	}
	
	public function actionRegist()
	{
		$model=new User;

		if(isset($_POST['User']))
		{
			$model->attributes=$_POST['User'];
			$model->password = md5($model->password);
			if($model->save())
			{
				$_SESSION["userName"] = $model->user_name;
				echo 'success';
			}
			else
			{
				$arr = $model->getErrors();
				foreach ($arr as $key => $value) {
					echo $value[0];
					break;
				}
			}
		}
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Menu']))
		{
			$model->attributes=$_POST['Menu'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}
	
	public function getUserModel()
	{
		$model = new User();
		$model->gender = '1';
		return $model;
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$user = new User();
		$login = new User();
		$user->gender = 1;
		$this->render('index', array(
			'user'=>$user,
			'login'=>$login
		));
	}
	
	public function actionDetail()
	{
		$this->render('_view');
	}
	
	public function getContent($id)
	{
		$data =  new CActiveDataProvider('Content', array(
			'criteria'=>array(
				'condition'=>'owner_menu_id='.$id
			),
			'pagination'=>array(
				'pageSize'=>100
			)
		));
		
		return $data;
	}
	
	public function getPhoto()
	{
		return new CActiveDataProvider('Photo', array(
			'criteria'=>array(
				'condition'=>''
			),
			'pagination'=>array(
				'pageSize'=>100
			)
		));
	}
	
	public function actionView($id)
	{
		$this->layout = "//layouts/view";
		$this->render('view');
	}
	
	public function actionPhoto()
	{
		$this->layout = "//layouts/view_no_sub_menu";
		$this->render('photo');
	}
	
	public function actionFav()
	{
		$this->layout = "//layouts/view_no_sub_menu";
		$this->render('fav');
	}
	
	public function actionFavc($id)
	{
		$this->layout = "//layouts/view_no_sub_menu";
		
		$model=Content::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		
		$this->render('favc', array(
			"model"=>$model
		));
	}
	
	public function actionNews()
	{
		$this->layout = "//layouts/view_no_sub_menu";
		$this->render('news');
	}
	
	public function getNavMenus()
	{
		return new CActiveDataProvider('Menu', array(
			'criteria'=>array(
				'condition'=>'visible=1 and show_type=1'
			),
			'pagination'=>array(
				'pageSize'=>100
			)
		));
	}
	
	public function getOtherMenus()
	{
		return new CActiveDataProvider('Menu', array(
			'criteria'=>array(
				'condition'=>'visible=1 and show_type<>1'
			),
			'pagination'=>array(
				'pageSize'=>100
			)
		));
	}
	
	public function getBottoms()
	{
		return new CActiveDataProvider('Content', array(
			'criteria'=>array(
				'condition'=>'owner_menu_id = 0'
			),
			'pagination'=>array(
				'pageSize'=>100
			)
		));
	}
	
	public function getFavs()
	{
		return new CActiveDataProvider('Content', array(
			'criteria'=>array(
				'condition'=>'owner_menu_id = 17'
			),
			'pagination'=>array(
				'pageSize'=>10
			)
		));
	}
	
	public function getNews($page)
	{
		return new CActiveDataProvider('News', array(
			'criteria'=>array(
				'condition'=>''
			),
			'pagination'=>array(
				'pageSize'=>10,
				'currentPage'=>($page-1)
			)
		));
	}
	
	public function getQuestions()
	{
		return new CActiveDataProvider('Question', array(
			'criteria'=>array(
				'condition'=>''
			),
			'pagination'=>array(
				'pageSize'=>1000
			)
		));
	}
	
	public function getSuggests()
	{
		return new CActiveDataProvider('Suggest', array(
			'criteria'=>array(
				'condition'=>''
			),
			'pagination'=>array(
				'pageSize'=>1000
			)
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Menu('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Menu']))
			$model->attributes=$_GET['Menu'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Menu the loaded model
	 * @throws CHttpException
	 */
	public function actionContent($id)
	{
		$this->layout = "//layouts/view_no_sub_menu";
		$model=News::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		$this->render('news-content',array(
			'news'=>$model,
		));
	}
	
	public function actionQuestion()
	{
		$this->layout = "//layouts/view_no_sub_menu";
		$this->render('question');
	}
	
	public function actionSuggest()
	{
		$this->layout = "//layouts/view_no_sub_menu";
		$this->render('suggest');
	}
	
	public function actionAddSuggest()
	{
		$model=new Suggest();
		$this->layout = "//layouts/view_no_sub_menu";
		
		if(isset($_POST['Suggest']))
		{
			$model->attributes=$_POST['Suggest'];
			$model->submit_time=date('Y-m-d H:i:s');
			if($model->save())
				$this->redirect(array('suggest'));
		}
		$this->render('add-suggest', array(
			'model'=>$model
		));
	}
	
	public function actions()
    { 
            return array( 
                    'captcha'=>array(
                            'class'=>'CCaptchaAction',
                            'backColor'=>0xFFFFFF, 
                            'maxLength'=>'4',       // 最多生成几个字符
                            'minLength'=>'4',       // 最少生成几个字符
                            'height'=>'30',
            				'width'=>'55',
            				'offset'=>'0',
            				'testLimit'=>1
            				//'transparent'=>'true'
                    ), 
            ); 
    }

	/**
	 * Performs the AJAX validation.
	 * @param Menu $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['User']))
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
